4. Objects and Classes (1)

Lab Exercise 4-2: Create a Simple Banking Package

Objective

This exercise will introduce you to the banking project which we will return to in several labs. This project will (eventually) consist of a bank with several customers with several account each and a report. These classes will evolve over the next lab exercises.

In this exercise, you will create very simple version of the Account class. You will place this source file in the banking package. A test program, TestBanking, has been written in the default package that creates a single account. It initializes the balance of that account and performs several simple transactions. Finally, the test program displays the final balance of the account.

The UML Model of the banking Package

Directions

Start by changing your working directory to lab4/exercise2 on your computer.

  1. Create the banking directory in your working directory.
  2. Create the Account class in the file Account.java under the banking directory. This class must implement the model in the above UML diagram.
    1. declare one private object attribute: balance; this attribute will hold the current (or "running") balance of the bank account
    2. declare a public constructor that takes one parameter (init_balance); that populates the balance attribute
    3. declare a public method getBalance that retrieves the current balance
    4. declare a public method deposit that adds the amount parameter to the current balance
    5. declare a public method withdraw that removes the amount parameter from the current balance
  3. Compile the given TestBanking.java file. This has a cascading effect of compiling all of the classes used in the program; thus compiling the Customer.java and Account.java files under the banking directory.
    javac -d . TestBanking.java
  4. Run the TestBanking class. You shoud see the following output:
    Creating an account with a 500.00 balance.
    Withdraw 150.00
    Deposit 22.50
    Withdraw 47.62
    The account has a balance of 324.88